package org.shagin.jfin.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

/**
 * The Class DateUtils.
 */
public class DateUtils {

    private static final int YEAR_1900 = 1900;
    public static final String DATE_FORMAT = "dd.MM.yyyy";
    public static final Date OPEN_DATE = getOpenDate();
    public static final Date CLOSE_DATE = getCloseDate();
    private static final SimpleDateFormat fmt = getSimpleDateFormat();

    /**
     * Tomorrow.
     * 
     * @param today the today
     * @return the date
     */
    public static Date tomorrow(Date today) {
        return incDay(today, 1);
    }

    /**
     * Yesterday.
     * 
     * @param today the today
     * @return the date
     */
    public static Date yesterday(Date today) {
        return incDay(today, -1);
    }


    /**
     * Inc day.
     *
     * @param date the date
     * @param delta the delta
     * @return the date
     */
    public static Date incDay(Date date, int delta) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, delta);
        return cal.getTime();
    }
    
    /**
     * Inc month.
     *
     * @param date the date
     * @param delta the delta
     * @return the date
     */
    public static Date incMonth(Date date, int delta) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, delta);
        return cal.getTime();
    }

    /**
     * Date.
     * 
     * @param year the year
     * @param month the month
     * @param day the day
     * @return the date
     */
    @SuppressWarnings("deprecation")
    public static Date date(int year, int month, int day) {
        return new Date(year - YEAR_1900, month, day);
    }

    /**
     * Now.
     * 
     * @return the date
     */
    public static Date now() {
        return new Date();
    }

    /**
     * Gets the open date.
     * 
     * @return the open date
     */
    public static Date getOpenDate() {
        return date(2100, Calendar.DECEMBER, 31);
    }

    /**
     * Gets the close date.
     * 
     * @return the close date
     */
    public static Date getCloseDate() {
        return date(1900, Calendar.DECEMBER, 31);
    }

    /**
     * Gets the simple date format.
     * 
     * @return the simple date format
     */
    public static SimpleDateFormat getSimpleDateFormat() {
        return new SimpleDateFormat(DATE_FORMAT);
    }

    /**
     * Parses the.
     * 
     * @param source the source
     * @return the date
     */
    public static Date parse(String source) {
        try {
            return fmt.parse(source);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Format.
     * 
     * @param date the date
     * @return the string
     */
    public static String format(Date date) {
        return fmt.format(date);
    }

    /**
     * Checks if is equal.
     * 
     * @param date1 the date1
     * @param date2 the date2
     * @return true, if is equal
     */
    public static boolean isEqual(Date date1, Date date2) {
        SimpleDateFormat fmt = getSimpleDateFormat();
        return fmt.format(date1).equals(fmt.format(date2));
    }

    /**
     * Gets the week day.
     * 
     * @param weekDay the week day
     * @return the week day
     */
    public static String getWeekDay(int weekDay) {
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        Calendar cal = Calendar.getInstance();
        cal.set(2011, Calendar.OCTOBER, 16);
        cal.add(Calendar.DAY_OF_YEAR, (weekDay - 1));
        return StringUtils.capitalize(sdf.format(cal.getTime()));
    }

    /**
     * Gets the month.
     * 
     * @param month the month
     * @return the month
     */
    public static String getMonth(int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("MMM");
        Calendar cal = Calendar.getInstance();
        cal.set(2011, month, 16);
        return StringUtils.capitalize(sdf.format(cal.getTime()));
    }

}
